package com.venus.finance.account.service.impl;

import java.util.List;

import com.venus.common.service.impl.BaseServiceImpl;
import com.venus.common.utils.ConditionUtil;
import com.venus.finance.account.dao.IAccountDAO;
import com.venus.finance.account.service.IAccountService;
import com.venus.model.AccountVO;
import com.venus.model.PubUserGroupVO;
import com.venus.model.PubUserVO;

public class AccountServiceImpl extends BaseServiceImpl implements
		IAccountService {

	private IAccountDAO accountDAO;
	
	public void setAccountDAO(IAccountDAO accountDAO) {
		this.accountDAO = accountDAO;
	}

	@Override
	public AccountVO loadAccountVO(Integer iAccountId) {
		return this.accountDAO.loadModel(AccountVO.class, iAccountId);
	}
	
	@Override
	public void saveAccountVO(AccountVO objAccountVO) {
		this.accountDAO.saveModel(objAccountVO);
	}
	
	@Override
	public void saveOrUpdateAccountVO(AccountVO objAccountVO) {
		this.accountDAO.saveOrUpdateModel(objAccountVO);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AccountVO> findAccountByUser(PubUserVO objPubUserVO){
		return this.accountDAO.find("from AccountVO where pubUser=? order by sort", objPubUserVO);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<AccountVO> findAccountByGroup(PubUserGroupVO objPubUserGroupVO) {
		return this.accountDAO.find("from AccountVO where pubUser.userGroup=? order by sort", objPubUserGroupVO);
	}
	
	@Override
	public void deleteAccountVO(String[] strAccountIds) {
		for(String strAccountId : strAccountIds){
			int iAccountId = Integer.parseInt(strAccountId);
			// 如果在系统中没有对此账户的应用才可以被删除
			// 费用记录
			Long lFeeCount = this.accountDAO.exeCountHql("select count(feeVO.feeId) from FeeVO feeVO where feeVO.account.accountId=?", iAccountId);
			if(lFeeCount>0){
				this.log.info("账户包含费用记录,不能被删除!账户编号为:"+iAccountId);
				return ;
			}
			// 债权债务记录,包含转入账户和转出账户
			Long lCreditDebtCount = this.accountDAO.exeCountHql("select count(creditDebtVO.creditDebtId) from CreditDebtVO creditDebtVO where creditDebtVO.outAccount.accountId=? or creditDebtVO.inAccount.accountId=?", iAccountId, iAccountId);
			if(lCreditDebtCount>0){
				this.log.info("账户包含债权债务记录,不能被删除!账户编号为:"+iAccountId);
				return ;
			}
			// 转账记录,包含转入账户和转出账户
			Long lTransferAccountCount = this.accountDAO.exeCountHql("select count(transferAccountVO.transferAccountId) from TransferAccountVO transferAccountVO where transferAccountId.fromAccount.accountId=? or transferAccountId.toAccount.accountId=?", iAccountId, iAccountId);
			if(lTransferAccountCount>0){
				this.log.info("账户包含转账记录,不能被删除!账户编号为:"+iAccountId);
				return ;
			}
			// 删除账户信息
			this.accountDAO.delete("delete from AccountVO where accountId=?", iAccountId);
		}
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<AccountVO> findAccountVOs(List<ConditionUtil> lstConditionUtils) {
		return this.accountDAO.exeHql("from AccountVO where 1=1 ", lstConditionUtils);
	}

}
